package pref.junx.flybird.network;

import okhttp3.OkHttpClient;
import pref.junx.flybird.app.AppContext;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

public abstract class RetrofitUtils {

    // Retrofit实例
    private static Retrofit retrofit;

    // OkHttp实例
    private static OkHttpClient okHttpClient;

    /**
     * 获取Retrofit
     * @return
     */
    protected static Retrofit getRetrofit() {

        if(null == retrofit) {

            if(null == okHttpClient) okHttpClient = OkHttpUtils.getOkHttpClient();

            retrofit = new Retrofit.Builder()
                    // 设置服务器地址
                    .baseUrl(AppContext.HOST_URL)
                    // 添加转化库, 采用Gson
                    .addConverterFactory(GsonConverterFactory.create())
                    // 添加回调库, 采用RxJava
                    .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                    // 设置使用OkHttp网络请求
                    .client(okHttpClient)
                    .build();
        }
        return retrofit;
    }

}
